from django.db import models
from django.utils.timezone import now
# Create your models here.

# 用户模型类
class UserModel(models.Model):
    username = models.CharField('用户名',max_length=50)
    password = models.CharField('密码',max_length=200)
    phone = models.CharField('手机号',max_length=11,unique=True)
    created_data = models.DateTimeField(default=now(),verbose_name='新增时间')
    finally_data = models.DateTimeField(verbose_name='最后登入时间',null=True,blank=True)
    img = models.CharField('用户头像',max_length=200,null=True,blank=True)
    email = models.EmailField('邮箱',null=True,default=None)
    is_super_user = models.BooleanField(default=0,verbose_name='是否为管理员',null=True,blank=True)


    def __str__(self):
        return self.username

    class Meta:
        db_table = 'tu_user'
        verbose_name_plural = '用户表'


# 权限类型
class PermissionType(models.Model):
    name = models.CharField('权限类型',max_length=50)

    class Meta:
        db_table = 'tb_permission_type'

    def __str__(self):
        return self.name


class Permission(models.Model):
    """权限表"""
    name = models.CharField(max_length=30, verbose_name='权限名称')
    code_name = models.CharField(max_length=300, verbose_name='权限识别名称')
    type_name = models.ForeignKey(PermissionType, on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_permission'

    def __str__(self):
        return self.name


class UserGroup(models.Model):
    """用户组"""
    name = models.CharField(max_length=30, verbose_name='组名称')
    user = models.ManyToManyField(UserModel)
    permission = models.ManyToManyField(Permission)

    class Meta:
        db_table = 'tb_user_group'

    def __str__(self):
        return self.name


